<?php
session_start();
header('content-type: text/html;charset=utf-8');
cnVerification(200, 60);

//数字验证码
function cnVerification($width = 100, $heigth = 30, $point = 200, $line = 3)
{
    $image   = imagecreatetruecolor($width, $heigth);
    $bgcolor = imagecolorallocate($image, 255, 255, 255);
    imagefill($image, 0, 0, $bgcolor);
    $fontface = './simkai.ttf';
    $str      = "你大爷的月让步拉萨电力缺口巨大期望看见我开发年前旅客往返年轻父母雷克萨解放路微积分前国防预算不错呢阿莱克斯江东父老康师傅你我阿胶排骨饭我饿放牛娃能否李维嘉路过那里弄撒和福利和污染了国家为了你们了我年龄放宽我纳滤膜外婆恶搞很快就为了看斯洛克了我科普馆勤务体育统考多年从事";
    $strdb    = str_split($str, 3);

    $code     = '';
    for ($i = 0; $i < 4; $i++) {
        $fontcolor = imagecolorallocate($image, rand(0, 120), rand(0, 120), rand(0, 120));
        $cn        = $strdb[rand(0,count($strdb))];
        $code      .= $cn;
        imagettftext($image, mt_rand(20, 24), mt_rand(-60, 60), (40 * $i + 20), mt_rand(30, 35), $fontcolor, $fontface, $cn);
    }
    $_SESSION['cn_code'] = $code;
    //增加点
    for ($i = 0; $i < $point; $i++) {
        $pointcolor = imagecolorallocate($image, rand(50, 200), rand(50, 200), rand(50, 200));
        imagesetpixel($image, rand(1, $width - 1), rand(1, $heigth - 1), $pointcolor);
    }
    //增加线
    for ($i = 0; $i < $line; $i++) {
        $linecolor = imagecolorallocate($image, rand(80, 220), rand(80, 220), rand(80, 220));
        imageline($image, rand(1, $width - 1), rand(1, $heigth - 1), rand(1, $width - 1), rand(1, $heigth - 1), $linecolor);
    }
    header('content-type: image/png');
    imagepng($image);
    imagedestroy($image);
}

